package com.yjha.lzmarathonsys.service.impl;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.yjha.lzmarathonsys.bean.*;
import com.yjha.lzmarathonsys.dto.Param;
import com.yjha.lzmarathonsys.mapper.TbPersonClockinMapper;
import com.yjha.lzmarathonsys.service.*;
import com.yjha.lzmarathonsys.utils.*;
import com.yjha.lzmarathonsys.vo.TbPersonClockinVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.*;
import java.util.stream.Collectors;


@Service("tbPersonClockinService")
public class TbPersonClockinServiceImpl extends ServiceImpl<TbPersonClockinMapper, TbPersonClockin> implements TbPersonClockinService {

	public static final String Authorization = "Authorization";
	@Value("${lzmarathonsys.personClockin.codePrefix}")
	private String codePrefix;
	@Value("${lzmarathonsys.personClockin.getmyClockin}")
	private String getmyClockin;
	@Value("${lzmarathonsys.personClockin.getAll}")
	private String getAll;
	@Value("${lzmarathonsys.personClockin.getList}")
	private String getList;
	@Autowired
	private TbCommentService tbCommentService;
	@Autowired
	private TbLikeService tbLikeService;
	@Autowired
	private TbImageslService tbImageslService;
	@Autowired
	private TbUserInfoService tbUserInfoService;
	@Autowired
	private FileUploadUtil fileUploadUtil;
	@Autowired
	private HttpServletRequest request;
	@Autowired
	private TbPersonClockinMapper tbPersonClockinMapper;
	@Autowired
	private RedisUtils redisUtils;

	@Override
	public PageUtils queryPage(Param params) {
		// PageUtils pageUtils = null;
		// boolean hasKey = redisUtils.hasKey(getList);
		// if (!hasKey) {
		Map<String, Object> param = new HashMap<>();
		String page1 = params.getPage();
		String limit1 = params.getLimit();
		param.put("page", page1);
		param.put("limit", limit1);
		QueryWrapper<TbPersonClockin> tbPersonClockinQueryWrapper = new QueryWrapper<>();
		if (StringUtils.isNotBlank(params.getLongitudeLatitude())) {
			tbPersonClockinQueryWrapper.like("tb_longitude_latitude", params.getLongitudeLatitude());
		}
		if (StringUtils.isNotBlank(params.getTbCreatetime())) {
			tbPersonClockinQueryWrapper.and(wapper -> {
				wapper.eq("tb_createtime", params.getTbCreatetime());
			});
		}
		if (StringUtils.isNotBlank(params.getTbStatus())) {
			tbPersonClockinQueryWrapper.and(wapper -> {
				wapper.eq("tb_status", params.getTbStatus());
			});
		}

		IPage<TbPersonClockin> page = this.page(
				new Query<TbPersonClockin>().getPage(param),
				tbPersonClockinQueryWrapper.orderByDesc("tb_createtime")
		);
		PageUtils pageUtils = new PageUtils(page);
		List<TbPersonClockinVo> collect = page.getRecords().stream().map(tbPersonClockin -> {
			QueryWrapper<TbUserInfo> tbUserInfoQueryWrapper = new QueryWrapper<>();
			tbUserInfoQueryWrapper.eq("tb_wxuuid", tbPersonClockin.getTbUserid());
			TbUserInfo tbUserInfo = tbUserInfoService.getOne(tbUserInfoQueryWrapper);
			TbPersonClockinVo tbPersonClockinVo = new TbPersonClockinVo();
			BeanUtils.copyProperties(tbPersonClockin, tbPersonClockinVo);
			QueryWrapper<TbImagesl> tbImageslQueryWrapper = new QueryWrapper<TbImagesl>().eq("tb_person_uuid", tbPersonClockinVo.getTbUuid());
			List<TbImagesl> tbImageslList = tbImageslService.list(tbImageslQueryWrapper);
			QueryWrapper<TbLike> likeQueryWrapper = new QueryWrapper<TbLike>().eq("target_id", tbPersonClockinVo.getTbId());
			long count = tbLikeService.count(likeQueryWrapper);
			QueryWrapper<TbComment> tbCommentQueryWrapper = new QueryWrapper<TbComment>().eq("tb_target_id", tbPersonClockinVo.getTbId());
			Long counted = tbCommentService.count(tbCommentQueryWrapper);
			tbPersonClockinVo.setTbImage(tbUserInfo.getTbImage());
			tbPersonClockinVo.setTbUsername(tbUserInfo.getTbUsername());
			tbPersonClockinVo.setLikeNum(count);
			tbPersonClockinVo.setCommentNum(counted);
			tbPersonClockinVo.setTbImageslList(tbImageslList);
			return tbPersonClockinVo;
		}).sorted((o1, o2) -> o2.getTbCreatetime().compareTo(o1.getTbCreatetime())).collect(Collectors.toList());
		pageUtils.setList(collect);
		//     try {
		//         redisUtils.set(getList, pageUtils,30*60);
		//     } catch (Exception e) {
		//         throw new RuntimeException(e);
		//     }
		// }else{
		//     pageUtils = (PageUtils) redisUtils.get(getList);
		// }
		return pageUtils;
	}

	@Override
	public R getTbPersonClockinById(Integer tbId) {
		String openId = JwtUtils.getOpenId(JwtUtils.getTokenFromRequest(request));
		TbPersonClockin tbPersonClockin = this.getOne(new QueryWrapper<TbPersonClockin>().eq("tb_id", tbId));
		List<TbImagesl> list = null;
		TbPersonClockinVo tbPersonClockinVo = null;
		if (tbPersonClockin != null) {
			QueryWrapper<TbUserInfo> tbUserInfoQueryWrapper = new QueryWrapper<>();
			tbUserInfoQueryWrapper.eq("tb_wxuuid", tbPersonClockin.getTbUserid());
			TbUserInfo tbUserInfo = tbUserInfoService.getOne(tbUserInfoQueryWrapper);
			QueryWrapper<TbImagesl> tbImageslQueryWrapper = new QueryWrapper<TbImagesl>().eq("tb_person_uuid", tbPersonClockin.getTbUuid());
			list = tbImageslService.list(tbImageslQueryWrapper);
			QueryWrapper<TbLike> likeQueryWrapper = new QueryWrapper<TbLike>().eq("target_id", tbPersonClockin.getTbUuid());
			long count = tbLikeService.count(likeQueryWrapper);
			QueryWrapper<TbLike> likeQueryWrapper1 = new QueryWrapper<TbLike>().eq("target_id", tbPersonClockin.getTbUuid()).and(tw -> tw.eq("user_id", openId));
			TbLike tbLike = tbLikeService.getOne(likeQueryWrapper1);// QueryWrapper<TbComment> commentQueryWrapper = new QueryWrapper<TbComment>().eq("tb_target_id", tbPersonClockin.getTbId()).and(tbCommentQueryWrapper1 -> {
			//     tbCommentQueryWrapper1.eq("tb_type", 0);
			// });
			QueryWrapper<TbComment> commentQueryWrapper2 = new QueryWrapper<TbComment>().eq("tb_target_id", tbPersonClockin.getTbId()).and(tbCommentQueryWrapper1 -> {
				tbCommentQueryWrapper1.eq("tb_type", 0).and(tw -> {
					tw.eq("tb_user_id", openId);
				});
			});
			QueryWrapper<TbComment> commentQueryWrapper1 = new QueryWrapper<TbComment>().eq("tb_target_id", tbPersonClockin.getTbId()).and(tbCommentQueryWrapper1 -> {
				tbCommentQueryWrapper1.eq("tb_type", 0);
			}).and(tw -> tw.eq("tb_target_status", 1));
			;
			tbPersonClockinVo = new TbPersonClockinVo();
			BeanUtils.copyProperties(tbPersonClockin, tbPersonClockinVo);
			tbPersonClockinVo.setLikeNum(count);
			tbPersonClockinVo.setTbImage(tbUserInfo.getTbImage());
			tbPersonClockinVo.setTbUsername(tbUserInfo.getTbUsername());
			tbPersonClockinVo.setTbCreatetime(tbPersonClockin.getTbCreatetime());
			tbPersonClockinVo.setCommentNum(tbCommentService.count(commentQueryWrapper1));
			tbPersonClockinVo.setTbImageslList(list);
			// tbPersonClockinVo.setTbCommentList(tbCommentService.list(commentQueryWrapper));
			List<TbComment> list1 = tbCommentService.list(commentQueryWrapper2);
			if (tbLike != null) {
				tbPersonClockinVo.setClockStatus(true);
			} else {
				tbPersonClockinVo.setClockStatus(false);
			}

			// String tbImageids = tbPersonClockin.getTbImageids();
			// if (tbImageids != null && !"".equals(tbImageids)) {
			//     String[] tbImageid = tbImageids.split(",");
			//     QueryWrapper<TbImagesl> imageslQueryWrapper = new QueryWrapper<TbImagesl>().in("tb_id", tbImageid);
			//     list = tbImageslService.list(imageslQueryWrapper);
			// }
		}
		return R.ok().put("tbPersonClockin", tbPersonClockinVo);
	}

	@Override
	public R getmyClockinById(Param params) {
		// PageUtils pageUtils = null;
		// boolean hasKey = redisUtils.hasKey(getmyClockin);
		// if (!hasKey) {
		// JwtUtils.getTokenFromRequest(request);
		// JwtUtils.getOpenId(JwtUtils.getTokenFromRequest(request));
		String tokenFromRequest = JwtUtils.getTokenFromRequest(request);
		String temp = JwtUtils.getOpenId(tokenFromRequest);
		String page1 = params.getPage();
		String limit1 = params.getLimit();
		Map<String, Object> param = new HashMap<>();
		param.put("page", page1);
		param.put("limit", limit1);
		MPJLambdaWrapper<TbPersonClockin> tbPersonClockinMPJLambdaWrapper = new MPJLambdaWrapper<>();
		tbPersonClockinMPJLambdaWrapper
				.select(TbUserInfo::getTbUsername, TbUserInfo::getTbImage, TbUserInfo::getTbWxuuid)
				.select(TbPersonClockin::getTbId, TbPersonClockin::getTbCreatetime, TbPersonClockin::getTbUuid, TbPersonClockin::getTbLocation, TbPersonClockin::getTbLongitudeLatitude, TbPersonClockin::getTbTestimonials, TbPersonClockin::getTbStatus)
				.leftJoin(TbUserInfo.class, TbUserInfo::getTbWxuuid, TbPersonClockin::getTbUserid)
				.eq(TbPersonClockin::getTbUserid, temp);
		IPage<TbPersonClockinVo> page = tbPersonClockinMapper.selectJoinPage(
				new Page<>(Long.parseLong((String) param.get("page")), Long.parseLong((String) param.get("limit"))),
				TbPersonClockinVo.class,
				tbPersonClockinMPJLambdaWrapper.orderByDesc("tb_createtime")
		);
		PageUtils pageUtils = new PageUtils(page);
		List<TbPersonClockinVo> collect = page.getRecords().stream().map(tbPersonClockinVo -> {
			QueryWrapper<TbLike> tbLikeQueryWrapper = new QueryWrapper<>();
			QueryWrapper<TbImagesl> tbImageslQueryWrapper = new QueryWrapper<TbImagesl>().eq("tb_person_uuid", tbPersonClockinVo.getTbUuid());
			List<TbImagesl> tbImageslList = tbImageslService.list(tbImageslQueryWrapper);
			QueryWrapper<TbLike> likeQueryWrapper = new QueryWrapper<TbLike>().eq("target_id", tbPersonClockinVo.getTbUuid());
			long count = tbLikeService.count(likeQueryWrapper);
			QueryWrapper<TbComment> tbCommentQueryWrapper = new QueryWrapper<TbComment>().eq("tb_target_id", tbPersonClockinVo.getTbId()).and(tw -> {
				tw.eq("tb_type", 0);
			}).and(tw -> tw.eq("tb_target_status", 1));
			Long counted = tbCommentService.count(tbCommentQueryWrapper);
			QueryWrapper<TbLike> and = tbLikeQueryWrapper.eq("target_id", tbPersonClockinVo.getTbUuid()).and(tbLikeQueryWrapper1 -> {
				tbLikeQueryWrapper1.eq("user_id", temp);
			});
			TbLike tbLike = tbLikeService.getOne(and);
			if (tbLike != null) {
				tbPersonClockinVo.setClockStatus(true);
			} else {
				tbPersonClockinVo.setClockStatus(false);
			}
			tbPersonClockinVo.setLikeNum(count);
			tbPersonClockinVo.setCommentNum(counted);
			tbPersonClockinVo.setTbImageslList(tbImageslList);
			return tbPersonClockinVo;
		}).sorted((o1, o2) -> o2.getTbCreatetime().compareTo(o1.getTbCreatetime())).collect(Collectors.toList());
		pageUtils.setList(collect);
		//     // return R.ok().put("page",pageUtils);
		//         try {
		//             redisUtils.set(getmyClockin, pageUtils,30*60);
		//         } catch (Exception e) {
		//             throw new RuntimeException(e);
		//         }
		// }else{
		//     pageUtils = (PageUtils) redisUtils.get(getmyClockin);
		// }
		return R.ok().put("page", pageUtils);
	}

	@Override
	public R getAll(Param params) {
		// PageUtils pageUtils = null;
		// boolean hasKey = redisUtils.hasKey(getAll);
		// if (!hasKey) {
		String tokenFromRequest = JwtUtils.getTokenFromRequest(request);
		String temp = JwtUtils.getOpenId(tokenFromRequest);
		String page1 = params.getPage();
		String limit1 = params.getLimit();
		Map<String, Object> param = new HashMap<>();
		param.put("page", page1);
		param.put("limit", limit1);
		// List<String> longitudeLatitudeList = new ArrayList<>();
		List<String> longitudeLatitudeList = params.getLongitudeLatitudeList();
		// longitudeLatitudeList.add(params.getLongitudeLatitude());
		MPJLambdaWrapper<TbPersonClockin> tbPersonClockinMPJLambdaWrapper = new MPJLambdaWrapper<>();
		tbPersonClockinMPJLambdaWrapper
				.select(TbUserInfo::getTbUsername, TbUserInfo::getTbImage)
				.select(TbPersonClockin::getTbId, TbPersonClockin::getTbCreatetime, TbPersonClockin::getTbUuid, TbPersonClockin::getTbLocation, TbPersonClockin::getTbLongitudeLatitude, TbPersonClockin::getTbTestimonials, TbPersonClockin::getTbStatus)
				.leftJoin(TbUserInfo.class, TbUserInfo::getTbWxuuid, TbPersonClockin::getTbUserid)
				.eq(TbPersonClockin::getTbStatus, 0).and(tw -> tw.in(TbPersonClockin::getTbLongitudeLatitude, longitudeLatitudeList));
		IPage<TbPersonClockinVo> page = tbPersonClockinMapper.selectJoinPage(
				new Page<>(Long.parseLong((String) param.get("page")), Long.parseLong((String) param.get("limit"))),
				TbPersonClockinVo.class,
				tbPersonClockinMPJLambdaWrapper
		);
		PageUtils pageUtils = new PageUtils(page);
		List<TbPersonClockinVo> collect = page.getRecords().stream().map(tbPersonClockinVo -> {
			QueryWrapper<TbImagesl> tbImageslQueryWrapper = new QueryWrapper<TbImagesl>().eq("tb_person_uuid", tbPersonClockinVo.getTbUuid());
			List<TbImagesl> tbImageslList = tbImageslService.list(tbImageslQueryWrapper);
			QueryWrapper<TbLike> likeQueryWrapper = new QueryWrapper<TbLike>().eq("target_id", tbPersonClockinVo.getTbUuid());
			long count = tbLikeService.count(likeQueryWrapper);
			QueryWrapper<TbComment> tbCommentQueryWrapper = new QueryWrapper<TbComment>().eq("tb_target_id", tbPersonClockinVo.getTbId()).and(tw -> {
				tw.eq("tb_type", 0);
			}).and(tw -> tw.eq("tb_target_status", 1));
			Long counted = tbCommentService.count(tbCommentQueryWrapper);
			QueryWrapper<TbLike> tbLikeQueryWrapper = new QueryWrapper<>();
			QueryWrapper<TbLike> and = tbLikeQueryWrapper.eq("target_id", tbPersonClockinVo.getTbUuid()).and(tbLikeQueryWrapper1 -> {
				tbLikeQueryWrapper1.eq("user_id", temp);
			});
			TbLike tbLike = tbLikeService.getOne(and);
			if (tbLike != null) {
				tbPersonClockinVo.setClockStatus(true);
			} else {
				tbPersonClockinVo.setClockStatus(false);
			}
			tbPersonClockinVo.setLikeNum(count);
			tbPersonClockinVo.setCommentNum(counted);
			tbPersonClockinVo.setTbImageslList(tbImageslList);
			return tbPersonClockinVo;
		}).sorted((o1, o2) -> o2.getTbCreatetime().compareTo(o1.getTbCreatetime())).collect(Collectors.toList());
		pageUtils.setList(collect);
		//     try {
		//         redisUtils.set(getAll, pageUtils,30*60);
		//     } catch (Exception e) {
		//         throw new RuntimeException(e);
		//     }
		// }else{
		//     pageUtils = (PageUtils) redisUtils.get(getAll);
		// }
		return R.ok().put("page", pageUtils);
	}

	@Transactional(rollbackFor = Exception.class)
	@Override
	public R saveTbPersonClockin(TbPersonClockin tbPersonClockin, MultipartFile[] files) throws Exception {
		String tokenFromRequest = JwtUtils.getTokenFromRequest(request);
		String openId = JwtUtils.getOpenId(tokenFromRequest);
		String uuid = UUID.randomUUID().toString().replace("-", "");
		tbPersonClockin.setTbUserid(openId);
		tbPersonClockin.setTbUuid(uuid);
		this.save(tbPersonClockin);
		List<String> uploadedUrls = new ArrayList<>();

		for (MultipartFile file : files) {
			// 类型校验
			String s;
			if ("docx".equalsIgnoreCase(FileUtil.getSuffix(file.getOriginalFilename()))) {
				s = fileUploadUtil.validateFile(file, "WD");
			} else {
				s = fileUploadUtil.validateFile(file, "TP");
			}

			if (s != null) {
				return R.error("文件校验失败：" + s);
			}

			// 构建路径
			String suffix = FileUtil.getSuffix(file.getOriginalFilename());
			String fileName = StrUtil.uuid().replaceAll("-", "") + "." + suffix;
			String uploadDir = fileUploadUtil.getWinOrLinuxUploadFilePath("tp");
			String fullPath = uploadDir + fileName;

			// 创建目录
			fileUploadUtil.createDirectory(uploadDir);

			// 保存文件
			file.transferTo(FileUtil.file(fullPath));

			// 构建 URL 返回
			String fileUrl = "https://jtfx.lzu.edu.cn/api/upload/tp/" + fileName;
			uploadedUrls.add(fileUrl);
		}
		for (int i = 0; i < uploadedUrls.size(); i++) {
			TbImagesl tbImagesl = new TbImagesl();
			tbImagesl.setTbImage(uploadedUrls.get(i));
			tbImagesl.setTbPersonUuid(uuid);
			tbImageslService.save(tbImagesl);
		}
		return R.ok();
	}

	@Override
	public R getLongitude() {// List<String> longitudeLatitude) {
		// Map<String, Object> tbPersonClockinVoHashMap = new HashMap<>();
		// List list = new ArrayList();
		// boolean hasKey = redisUtils.hasKey(codePrefix);
		// if (!hasKey) {
		QueryWrapper<TbPersonClockin> tbPersonClockinQueryWrapper = new QueryWrapper<>();
		tbPersonClockinQueryWrapper
				.select("count(*) as count", "tb_longitude_latitude", "tb_location").eq("tb_status", 0)
				// .in("tb_longitude_latitude", longitudeLatitude)  // 注意字段名和集合
				.groupBy("tb_longitude_latitude", "tb_location");

		List<Map<String, Object>> list1 = this.listMaps(tbPersonClockinQueryWrapper);
		List<TbPersonClockinVo> groupVoList = list1.stream().map(row -> {
			TbPersonClockinVo vo = new TbPersonClockinVo();
			vo.setCount(Long.parseLong(row.get("count").toString()));
			vo.setTbLongitudeLatitude((String) row.get("tb_longitude_latitude"));
			vo.setTbLocation((String) row.get("tb_location"));
			return vo;
		}).collect(Collectors.toList());
		// list.addAll(groupVoList);
		// tbPersonClockinVoHashMap.put("data",list);
		// redisUtils.hmset(codePrefix,tbPersonClockinVoHashMap,30*60);
		// }else {
		//     Map<Object, Object> map = redisUtils.hmget(codePrefix);
		//     return R.ok().put("data",map);
		// }
		return R.ok().put("data", groupVoList);
	}

	@Override
	public synchronized void audit(TbPersonClockin tbPersonClockin) {
		UpdateWrapper<TbPersonClockin> tbPersonClockinUpdateWrapper = new UpdateWrapper<>();
		tbPersonClockinUpdateWrapper.set("tb_status", tbPersonClockin.getTbStatus()).eq("tb_id", tbPersonClockin.getTbId());
		tbPersonClockinMapper.update(tbPersonClockin, tbPersonClockinUpdateWrapper);
	}

	@Transactional(rollbackFor = Exception.class)
	@Override
	public void removetbPersonClockinByIds(List<Integer> list) {
		for (int i = 0; i < list.size(); i++) {
			QueryWrapper<TbPersonClockin> imageslQueryWrapper = new QueryWrapper<TbPersonClockin>().eq("tb_id", list.get(i));
			TbPersonClockin tbPersonClockin = this.getOne(imageslQueryWrapper);
			QueryWrapper<TbImagesl> tbImageslQueryWrapper = new QueryWrapper<>();
			tbImageslQueryWrapper.eq("tb_person_uuid", tbPersonClockin.getTbUuid());
			List<TbImagesl> tbImagesls = tbImageslService.list(tbImageslQueryWrapper);
			for (int j = 0; j < tbImagesls.size(); j++) {
				this.removeById(list.get(i));
				String uploadDir = fileUploadUtil.getWinOrLinuxUploadFilePath("tp");
				// 创建 File 对象
				File file = new File(tbImagesls.get(j).getTbImage());
				// 获取文件名（不包含路径）
				String fileName = file.getName();
				// 替换为你实际的文件路径
				String filePath = uploadDir + fileName;
				try {
					File file1 = new File(filePath);
					file1.delete();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
			tbImageslService.remove(tbImageslQueryWrapper);
			tbImageslService.remove(tbImageslQueryWrapper);
			QueryWrapper<TbComment> tbCommentQueryWrapper = new QueryWrapper<>();
			tbCommentQueryWrapper.eq("tb_target_id", tbPersonClockin.getTbId()).and(tbCommentQueryWrapper1 -> {
				tbCommentQueryWrapper1.eq("tb_type", 0);
			});
			tbCommentService.remove(tbCommentQueryWrapper);
			QueryWrapper<TbLike> tbLikeQueryWrapper = new QueryWrapper<>();
			tbLikeQueryWrapper.eq("target_id", tbPersonClockin.getTbUuid());
			tbLikeService.remove(tbLikeQueryWrapper);
		}
	}

	@Override
	public PageUtils UnderList(Param params) {
		String tokenFromRequest = JwtUtils.getTokenFromRequest(request);
		String temp = JwtUtils.getOpenId(tokenFromRequest);
		QueryWrapper<TbLike> tbLikeQueryWrapper = new QueryWrapper<>();
		Map<String, Object> param = new HashMap<>();
		String page1 = params.getPage();
		String limit1 = params.getLimit();
		param.put("page", page1);
		param.put("limit", limit1);
		QueryWrapper<TbPersonClockin> tbPersonClockinQueryWrapper = new QueryWrapper<>();
		if (params.getTbCreatetime() != null && !"".equals(params.getTbCreatetime())) {
			tbPersonClockinQueryWrapper.and(wapper -> {
				wapper.eq("tb_createtime", params.getTbCreatetime());
			});
		}
		tbPersonClockinQueryWrapper.eq("tb_status", 2);
		IPage<TbPersonClockin> page = this.page(
				new Query<TbPersonClockin>().getPage(param),
				tbPersonClockinQueryWrapper.orderByDesc("tb_createtime")
		);
		PageUtils pageUtils = new PageUtils(page);
		List<TbPersonClockinVo> collect = page.getRecords().stream().map(tbPersonClockin -> {
			QueryWrapper<TbUserInfo> tbUserInfoQueryWrapper = new QueryWrapper<>();
			tbUserInfoQueryWrapper.eq("tb_wxuuid", tbPersonClockin.getTbUserid());
			TbUserInfo tbUserInfo = tbUserInfoService.getOne(tbUserInfoQueryWrapper);
			TbPersonClockinVo tbPersonClockinVo = new TbPersonClockinVo();
			BeanUtils.copyProperties(tbPersonClockin, tbPersonClockinVo);
			QueryWrapper<TbImagesl> tbImageslQueryWrapper = new QueryWrapper<TbImagesl>().eq("tb_person_uuid", tbPersonClockinVo.getTbUuid());
			List<TbImagesl> tbImageslList = tbImageslService.list(tbImageslQueryWrapper);
			QueryWrapper<TbLike> likeQueryWrapper = new QueryWrapper<TbLike>().eq("target_id", tbPersonClockinVo.getTbId());
			long count = tbLikeService.count(likeQueryWrapper);
			QueryWrapper<TbComment> tbCommentQueryWrapper = new QueryWrapper<TbComment>().eq("tb_target_id", tbPersonClockinVo.getTbId());
			Long counted = tbCommentService.count(tbCommentQueryWrapper);
			tbPersonClockinVo.setTbImage(tbUserInfo.getTbImage());
			tbPersonClockinVo.setTbUsername(tbUserInfo.getTbUsername());
			tbPersonClockinVo.setLikeNum(count);
			tbPersonClockinVo.setCommentNum(counted);
			tbPersonClockinVo.setTbImageslList(tbImageslList);
			return tbPersonClockinVo;
		}).sorted((o1, o2) -> o2.getTbCreatetime().compareTo(o1.getTbCreatetime())).collect(Collectors.toList());
		pageUtils.setList(collect);
		return pageUtils;
	}

	// @Override
	// public R getLongitudeData(List<String> longitudeLatitude) {
	//     Map<String, Object> tbPersonClockinVoHashMap = new HashMap<>();
	//     List list = new ArrayList();
	//     boolean hasKey = redisUtils.hasKey(codePrefix);
	//     if (!hasKey) {
	//         QueryWrapper<TbPersonClockin> tbPersonClockinQueryWrapper = new QueryWrapper<>();
	//         tbPersonClockinQueryWrapper
	//                 .select("count(*) as count", "tb_longitude_latitude", "tb_location")
	//                 // .in("tb_longitude_latitude", longitudeLatitude)  // 注意字段名和集合
	//                 .groupBy("tb_longitude_latitude", "tb_location");
	//
	//         List<Map<String, Object>> list1 = this.listMaps(tbPersonClockinQueryWrapper);
	//         List<TbPersonClockinVo> groupVoList = list1.stream().map(row -> {
	//             TbPersonClockinVo vo = new TbPersonClockinVo();
	//             vo.setCount(Long.parseLong(row.get("count").toString()));
	//             vo.setTbLongitudeLatitude((String) row.get("tb_longitude_latitude"));
	//             vo.setTbLocation((String) row.get("tb_location"));
	//             return vo;
	//         }).collect(Collectors.toList());
	//         list.addAll(groupVoList);
	//         tbPersonClockinVoHashMap.put("data",list);
	//         redisUtils.hmset(codePrefix,tbPersonClockinVoHashMap,100000L);
	//     }else {
	//         Map<Object, Object> map = redisUtils.hmget(codePrefix);
	//         return R.ok().put("data",map);
	//     }
	//     return R.ok().put("data",tbPersonClockinVoHashMap);
	// }
}